{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "dCew_VZqfvff"
   },
   "source": [
    "# Fonctions avancées d'Interface"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "hXpTCl18fvfh"
   },
   "source": [
    "Installez les bibliothèques 🤗 Transformers et 🤗 Gradio pour exécuter ce *notebook*."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "X_4rRGIofvfi"
   },
   "outputs": [],
   "source": [
    "!pip install datasets transformers[sentencepiece]\n",
    "!pip install gradio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "5qwMwbctnTNP"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "HKxbPoSqfvfk"
   },
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "import gradio as gr\n",
    "\n",
    "\n",
    "def chat(message, history):\n",
    "    history = history or []\n",
    "    if message.startswith(\"Combien\"):\n",
    "        response = random.randint(1, 10)\n",
    "    elif message.startswith(\"Comment\"):\n",
    "        response = random.choice([\"Super\", \"Bon\", \"Ok\", \"Mal\"])\n",
    "    elif message.startswith(\"Où\"):\n",
    "        response = random.choice([\"Ici\", \"Là\", \"Quelque part\"])\n",
    "    else:\n",
    "        response = \"Je ne sais pas.\"\n",
    "    history.append((message, response))\n",
    "    return history, history\n",
    "\n",
    "\n",
    "iface = gr.Interface(\n",
    "    chat,\n",
    "    [\"text\", \"state\"],\n",
    "    [\"chatbot\", \"state\"],\n",
    "    allow_screenshot=False,\n",
    "    allow_flagging=\"never\",\n",
    ")\n",
    "iface.launch()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "rNS-z93HnVRk"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "QSGjnAWpfvfn"
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "import tensorflow as tf\n",
    "\n",
    "import gradio as gr\n",
    "\n",
    "inception_net = tf.keras.applications.MobileNetV2()  # charger le modèle\n",
    "\n",
    "# Télécharger des étiquettes lisibles par l'homme pour ImageNet\n",
    "response = requests.get(\"https://git.io/JJkYN\")\n",
    "labels = response.text.split(\"\\n\")\n",
    "\n",
    "\n",
    "def classify_image(inp):\n",
    "    inp = inp.reshape((-1, 224, 224, 3))\n",
    "    inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp)\n",
    "    prediction = inception_net.predict(inp).flatten()\n",
    "    return {labels[i]: float(prediction[i]) for i in range(1000)}\n",
    "\n",
    "\n",
    "image = gr.Image(shape=(224, 224))\n",
    "label = gr.Label(num_top_classes=3)\n",
    "\n",
    "title = \"Classification des images avec Gradio + Exemple d'interprétation\"\n",
    "gr.Interface(\n",
    "    fn=classify_image, inputs=image, outputs=label, interpretation=\"default\", title=title\n",
    ").launch()"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "collapsed_sections": [],
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
